package com.jacklin.shopping.mall.service.Impl;

import com.google.common.collect.Lists;
import com.google.common.collect.Sets;
import com.lbz.common.ServerResponse;
import com.lbz.dao.CategoryMapper;
import com.lbz.entity.Category;
import com.lbz.service.CategoryService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Set;

/**
 * @Author 林必昭
 * @Date 2019/6/22 21:54
 */

@Service
@Slf4j
public class CategoryServiceImpl implements CategoryService {


    @Autowired
    private CategoryMapper categoryMapper;

    /**
     * 新增品类 逻辑实现
     *
     * @param categoryName
     * @param parentId
     * @return
     */
    @Override
    public ServerResponse addCategory(String categoryName, Integer parentId) {
        if (StringUtils.isBlank(categoryName) || parentId == null) {
            return ServerResponse.createByErrorMessage("添加品类参数错误");
        }
        Category category = new Category();
        category.setName(categoryName);
        category.setParentId(parentId);
        category.setStatus(true);   //这个分类是可用的

        int rowCount = categoryMapper.insert(category);
        if (rowCount > 0) {
            return ServerResponse.createBySuccess("新增品类成功");
        }
        return ServerResponse.createByErrorMessage("新增品类失败");

    }

    /**
     * 删除品类 逻辑实现
     *
     * @param categoryId
     * @return
     */
    @Override
    public ServerResponse<String>deleteCategory(Integer categoryId) {
        if (categoryId == null) {
            return ServerResponse.createByErrorMessage("删除品类参数错误");
        }
        int rowCount = categoryMapper.deleteByPrimaryKey(categoryId);
        if (rowCount > 0) {
            return ServerResponse.createBySuccessMessage("删除品类成功");
        }
        return ServerResponse.createByErrorMessage("删除品类失败");
    }

    /**
     * 更新品类名称 逻辑实现
     *
     * @param categoryId
     * @param categoryName
     * @return
     */
    @Override
    public ServerResponse updateCategoryName(Integer categoryId, String categoryName) {
        if (StringUtils.isBlank(categoryName) || categoryId == null) {
            return ServerResponse.createByErrorMessage("更新品类参数错误");
        }
        Category category = new Category();
        category.setId(categoryId);     //注意一定要将id设置先，才能更新类别名字
        category.setName(categoryName);
        int rowCount = categoryMapper.updateByPrimaryKeySelective(category);
        if (rowCount > 0) {
            return ServerResponse.createBySuccess("更新品类名称成功");
        }
        return ServerResponse.createByErrorMessage("更新品类名称失败");
    }

    /**
     * 获取品类子节点(平级) 逻辑实现
     * 当parentId为0，证明是根类节点
     *
     * @param categoryId
     * @return
     */
    @Override
    public ServerResponse<List<Category>> selectChildrenParallelCategory(Integer categoryId) {
        List<Category> categoryList = categoryMapper.selectCategoryChildrenByParentId(categoryId);
        if (CollectionUtils.isEmpty(categoryList)) {
            log.info("未找到当前分类的子分类");
        }
        return ServerResponse.createBySuccess(categoryList);
    }

    /**
     * 递归查询本节点的id以及孩子节点的id逻辑实现
     *
     * @param categoryId
     * @return
     */
    @Override
    public ServerResponse<List<Integer>> selectCategoryAndChildrenById(Integer categoryId) {
        Set<Category> categorySet = Sets.newHashSet();
        this.findChildCategoryRecursive(categorySet, categoryId);
        //最后是返回categoryId
        List<Integer> categoryIdList = Lists.newArrayList();
        if (categoryId != null) {
            for (Category categoryItem : categorySet) {
                categoryIdList.add(categoryItem.getId());
            }
        }
        return ServerResponse.createBySuccess(categoryIdList);

    }

    /**
     * 递归查询算法，自己调自己，我们使用Set集合做返回，可以排重，这里要重写Category的HashCode和equals
     * 的两个方法为什么呢？当两个对象相同，即equals返回true，则他们的hashCode一定相同，但是当两个对象的
     * hashCode相同，两个对象不一定相同，所以，得出结论当使用Set集合的时候，注意要重写equals和hashCode
     * 两个方法。
     */
    private Set<Category> findChildCategoryRecursive(Set<Category> categorySet, Integer categoryId) {
        Category category = categoryMapper.selectByPrimaryKey(categoryId);
        if (category != null) {
            categorySet.add(category);
        }
        //递归算法，查找子节点
        List<Category> categoryList = categoryMapper.selectCategoryChildrenByParentId(categoryId);
        for (Category categoryItem : categoryList) {
            //调用自己
            findChildCategoryRecursive(categorySet, categoryItem.getId());
        }
        return categorySet;
    }
}
